/*
 * RentalGUI.java
 *
 * Created on April 1, 2007, 4:37 PM
 */

package edu.byu.isys413.jjenkins.view;

import edu.byu.isys413.jjenkins.data.*;
import java.sql.Date;
import javax.swing.JOptionPane;

/**
 * This form is created to process the rental, which allows users to put in the date when the rental would be returned
 * @author Josh
 */
public class RentalGUI extends javax.swing.JFrame {
    
    RentalBO rental;
    
    /**
     * Creates new form RentalGUI
     */
    public RentalGUI() {
        initComponents();
    }
    
    private static RentalGUI instance = null;
    
    /**
     * Ensures that only one instance of the GUI exists at one time
     * @return RentalGUI
     */
    public static synchronized RentalGUI getInstance() {
        if (instance == null) {
            instance = new RentalGUI();
        }
        return instance;
    }
    
    
    /**
     * populates the GUI with information needed
     * @param rental RentalBO
     */
    public void populate(RentalBO rental){
        this.rental = rental;
        this.setVisible(true);
        this.setLocationRelativeTo(null);
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        txtdate = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        btnok = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Enter date to be returned");
        jLabel1.setMaximumSize(new java.awt.Dimension(34, 20));
        jLabel1.setMinimumSize(new java.awt.Dimension(34, 20));
        jLabel1.setPreferredSize(new java.awt.Dimension(34, 20));
        getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Format yyyy-mm-dd");
        jLabel2.setMaximumSize(new java.awt.Dimension(97, 20));
        jLabel2.setMinimumSize(new java.awt.Dimension(97, 20));
        jLabel2.setPreferredSize(new java.awt.Dimension(97, 20));
        jPanel1.add(jLabel2, java.awt.BorderLayout.NORTH);

        txtdate.setMaximumSize(new java.awt.Dimension(100, 20));
        txtdate.setMinimumSize(new java.awt.Dimension(100, 20));
        txtdate.setPreferredSize(new java.awt.Dimension(80, 20));
        txtdate.setRequestFocusEnabled(false);
        jPanel2.add(txtdate);

        btnok.setText("OK");
        btnok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnokActionPerformed(evt);
            }
        });

        jPanel4.add(btnok);

        jPanel2.add(jPanel4);

        jPanel1.add(jPanel2, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnokActionPerformed
// TODO add your handling code here:
        Date date = new Date(System.currentTimeMillis());
        rental.setDateOut(date);
        rental.setDateDue(date.valueOf(txtdate.getText()));
        rental.createDescription();
        rental.calcPrice();
        try{
            RevenueSourceFactory.getInstance().getDAO("rental").save(rental);
            
        }catch (DataException ex){
            JOptionPane.showMessageDialog(null, ex.toString(),"Cannot Save Rental", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
        
        NewOrder.getInstance().populateTransactionLines();
        this.dispose();
    }//GEN-LAST:event_btnokActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RentalGUI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnok;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField txtdate;
    // End of variables declaration//GEN-END:variables
    
}
